//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package templet;

import entity.Column;
import entity.Table;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ClientTempletUtil {
    public ClientTempletUtil() {
    }
    
    public static Map<String, String> getTempletList(String basePath) {
        Map<String, String> map = new HashMap();
        File root = new File(basePath);
        
        try {
            map = showAllFiles(basePath, root);
        } catch (Exception var4) {
            var4.printStackTrace();
        }
        
        return (Map)map;
    }
    
    static Map<String, String> showAllFiles(String basePath, File dir) throws Exception {
        Map<String, String> map = new HashMap();
        File[] fs = dir.listFiles();
        
        for(int i = 0; i < fs.length; ++i) {
            File file = new File(fs[i].getAbsolutePath());
            if (fs[i].isDirectory()) {
                try {
                    map.putAll(showAllFiles(basePath, fs[i]));
                } catch (Exception var7) {
                }
            } else {
                map.put(file.getName(), FileUtil.getContent(fs[i].getAbsolutePath()));
            }
        }
        
        return map;
    }
    
    public static String createContent(String oldContent, Map<String, String> map, Table table) {
        Iterator var4 = map.keySet().iterator();
        
        while(true) {
            String ks;
            String thf;
            do {
                if (!var4.hasNext()) {
                    oldContent = oldContent.replace("[table]", table.getName());
                    oldContent = oldContent.replace("[Table]", Utils.getClassName(table.getName()));
                    oldContent = oldContent.replace("[table2]", table.getName2());
                    oldContent = oldContent.replace("[Table2]", Utils.getClassName(table.getName2()));
                    oldContent = oldContent.replace("[comment]", table.getComment());
                    if (table.getKey() != null) {
                        oldContent = oldContent.replace("[key]", table.getKey());
                    }
                    
                    return oldContent;
                }
                
                ks = (String)var4.next();
                thf = "<" + ks + ">";
            } while(oldContent.indexOf(thf) < 0);
            
            String foreachContent = (String)map.get(ks);
            StringBuilder createContent = new StringBuilder();
            Iterator var9 = table.getColumns().iterator();
            
            while(var9.hasNext()) {
                Column column = (Column)var9.next();
                boolean b = true;
                if (ks.indexOf(".key") >= 0 && !column.getColumnKey().equals("PRI")) {
                    b = false;
                }
                
                if (ks.indexOf(".nokey") >= 0 && column.getColumnKey().equals("PRI")) {
                    b = false;
                }
                
                if (ks.indexOf(".String") >= 0 && !column.getColumnType().equals("String")) {
                    b = false;
                }
                
                if (b) {
                    System.out.println("替换符号内容：" + foreachContent);
                    String newContent = foreachContent.replace("[column]", column.getColumnName());
                    newContent = newContent.replace("[Column]", Utils.getClassName(column.getColumnName()));
                    newContent = newContent.replace("[column2]", column.getColumnName2());
                    newContent = newContent.replace("[Column2]", Utils.getClassName(column.getColumnName2()));
                    newContent = newContent.replace("[type]", column.getColumnType());
                    newContent = newContent.replace("[dbtype]", column.getColumnDbType());
                    newContent = newContent.replace("[columnComment]", column.getColumnComment());
                    createContent.append(newContent);
                    System.out.println("替换后内容：" + newContent);
                }
            }
            
            oldContent = oldContent.replace(thf, createContent.toString());
        }
    }
    
    public static String createContentForTable(String oldContent, Map<String, String> map, List<Table> tables) {
        Iterator var4 = map.keySet().iterator();
        
        while(true) {
            String ks;
            String thf;
            do {
                if (!var4.hasNext()) {
                    return oldContent;
                }
                
                ks = (String)var4.next();
                thf = "<" + ks + ">";
            } while(oldContent.indexOf(thf) < 0);
            
            String foreachContent = (String)map.get(ks);
            StringBuilder createContent = new StringBuilder();
            Iterator var9 = tables.iterator();
            
            while(var9.hasNext()) {
                Table table = (Table)var9.next();
                boolean b = true;
                if (b) {
                    String newContent = foreachContent.replace("[table]", table.getName());
                    newContent = newContent.replace("[Table]", Utils.getClassName(table.getName()));
                    newContent = newContent.replace("[table2]", table.getName2());
                    newContent = newContent.replace("[Table2]", Utils.getClassName(table.getName2()));
                    if (table.getKey() != null) {
                        newContent = newContent.replace("[key]", table.getKey());
                    }
                    
                    newContent = newContent.replace("[comment]", table.getComment());
                    createContent.append(newContent);
                }
            }
            
            oldContent = oldContent.replace(thf, createContent.toString());
        }
    }
    
    public static String createContent(String oldContent, Map<String, String> map) {
        String ks;
        for(Iterator var3 = map.keySet().iterator(); var3.hasNext(); oldContent = oldContent.replace("[" + ks + "]", (CharSequence)map.get(ks))) {
            ks = (String)var3.next();
        }
        
        return oldContent;
    }
}
